<template>
  <div class="app-container">
    <!-- 添加或修改客户跟进对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="客户id" prop="customId">
          <el-input v-model="form.customId" placeholder="请输入客户id" />
        </el-form-item>
        <el-form-item label="跟进人" prop="createBy">
          <el-input v-model="form.createBy" placeholder="请输入跟进人" />
        </el-form-item>
        <el-form-item label="沟通纪要" prop="record">
          <el-input v-model="form.record" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="跟进时间" prop="createTime">
          <el-date-picker clearable size="small"
            v-model="form.createTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择跟进时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getRecord, addRecord, updateRecord } from "@/api/custom/record";

export default {
  name: "RecordModal",
  data() {
    return {
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        createBy: [
          { required: true, message: "跟进人不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "跟进时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
  },
  methods: {
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        customId: null,
        createBy: null,
        record: null,
        createTime: null,
        trackStatus: null
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户跟进";
    },
    /** 修改按钮操作 */
    handleUpdate(id) {
      this.reset();
      getRecord(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户跟进";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRecord(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.$emit('refresh')
            });
          } else {
            addRecord(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.$emit('refresh')
            });
          }
        }
      });
    },
  }
};
</script>
